package com.yege.lab.book;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * 书籍
 * @author yege
 */
@RestController
public class BookController {
    private final static Log log = LogFactory.getLog(BookController.class);
    @Value("${book.img.prePath}")
    private String bookImgPrePath;
    @Autowired
    private BookService bookService;

    @PostMapping("/uploadBook")
    public String uploadBook(HttpServletRequest request,@RequestParam String bookName,@RequestParam String bookAuthor) {
        try {
            log.debug("uploadBook method was executed once");
            log.info("uploadBook method was executed once");
            log.warn("uploadBook method was executed once");
            log.error("uploadBook method was executed once");


            List<MultipartFile> multipartFileList = ((MultipartHttpServletRequest) request).getFiles("file");
            for (MultipartFile file : multipartFileList) {
                try {
                    byte[] buffer=file.getBytes();
                    String originalFilename=file.getOriginalFilename();
                    String fileHZ=originalFilename.split("\\.")[1];
                    String uuid=UUID.randomUUID().toString().replace("-", "");
                    String fileName=uuid+"."+fileHZ;
                    File targetFile = new File(bookImgPrePath+fileName);
                    OutputStream outStream = new FileOutputStream(targetFile);
                    outStream.write(buffer);
                    outStream.flush();
                    outStream.close();

                    BookDto book=new BookDto();
                    book.setBookName(bookName);
                    book.setBookAuthor(bookAuthor);
                    book.setBookImgName(fileName);
                    bookService.addBook(book);
                } catch (Exception ex) {
                }
                break;
            }
            return "success";
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return e.getMessage();
        }
    }

    @GetMapping("/book")
    public List<BookDto> queryBook() {
        try {
            log.debug("queryBook method was executed once");
            log.info("queryBook method was executed once");
            log.warn("queryBook method was executed once");
            log.error("queryBook method was executed once");
            return bookService.queryBook();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @GetMapping("/book/img")
    public void queryBookImg(@RequestParam String bookImgName, HttpServletResponse response) {
        log.debug("queryBookImg method was executed once");
        log.info("queryBookImg method was executed once");
        log.warn("queryBookImg method was executed once");
        log.error("queryBookImg method was executed once");
        FileInputStream fis = null;
        response.setContentType("image/gif");
        try {
            OutputStream out = response.getOutputStream();
            File targetFile = new File(bookImgPrePath+bookImgName);
            fis = new FileInputStream(targetFile);
            byte[] b = new byte[fis.available()];
            fis.read(b);
            out.write(b);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @GetMapping("/version")
    public String version() {
        log.debug("version method was executed once");
        log.info("version method was executed once");
        log.warn("version method was executed once");
        log.error("version method was executed once");
        return "book server running version is V1.0";
    }
}